package com.k.base.jdk.list;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public class FunctionDOTest {
    public static void main(String[] args) {
        List<FunctionDO> list = new ArrayList<>();
        FunctionDO vo11 = new FunctionDO("11","SMP-USER","用户管理","zh-CN","SMP");
        FunctionDO vo12 = new FunctionDO("12","SMP-USER","userMgr","en","SMP");
        FunctionDO vo13 = new FunctionDO("13","SMP-USER","userMgrBR","pt-BR","SMP");
        FunctionDO vo21 = new FunctionDO("21","SMP-AUTH","权限管理","zh-CN","SMP");
        FunctionDO vo22 = new FunctionDO("22","SMP-AUTH","authMgr","en","SMP");
        FunctionDO vo23 = new FunctionDO("23","SMP-AUTH","authMgrBR","pt-BR","SMP");

        FunctionDO vo31 = new FunctionDO("31","VCS-USER","用户管理","zh-CN","VCS");
        FunctionDO vo32 = new FunctionDO("32","VCS-USER","userMgr","en","VCS");
        FunctionDO vo33 = new FunctionDO("33","VCS-USER","userMgrBR","pt-BR","VCS");
        FunctionDO vo41 = new FunctionDO("41","VCS-AUTH","权限管理","zh-CN","VCS");
        FunctionDO vo42 = new FunctionDO("42","VCS-AUTH","authMgr","en","VCS");
        FunctionDO vo43 = new FunctionDO("43","VCS-AUTH","authMgrBR","pt-BR","VCS");
        list.add(vo11);list.add(vo12);list.add(vo13);list.add(vo21);list.add(vo22);list.add(vo23);
        list.add(vo31);list.add(vo32);list.add(vo33);list.add(vo41);list.add(vo42);list.add(vo43);
        Map<String,List<FunctionDO>> map = list.stream().collect(Collectors.groupingBy(FunctionDO::getSysCode));
        for(Map.Entry<String,List<FunctionDO>> entry:map.entrySet()){
            String k = entry.getKey();
            List<FunctionDO> value = entry.getValue();
            System.out.println("系统："+k);
            Map<String,List<FunctionDO>> map1 = value.stream().collect(Collectors.groupingBy(FunctionDO::getFuncCode));
            for(Map.Entry<String,List<FunctionDO>> entry1:map1.entrySet()){
                String fucCode = entry1.getKey();
                List<FunctionDO> list3 = entry1.getValue();
                System.out.println("功能菜单编码：" + fucCode);
                System.out.println(list3);

                String json = buildJson(list3);
                FunctionDO functionDONew = new FunctionDO();
                functionDONew.setGuid(UUID.randomUUID().toString());
                functionDONew.setFuncCode(list3.get(0).getFuncCode());
                functionDONew.setFuncName(json);
                functionDONew.setSysCode(list3.get(0).getSysCode());
                System.out.println(functionDONew.toSql());
                System.out.println("----------");
            }
            System.out.println("==========");
        }
    }

    private static String buildJson(List<FunctionDO> list3) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for(int i=0;i<list3.size();i++){
            FunctionDO itm = list3.get(i);
            String key = itm.getLanguageTag();
            String v = itm.getFuncName();
            sb.append("\"").append(key) .append("\"")
                    .append(":")
                    .append("\"").append(v).append("\"");
                    if(i!=list3.size()-1){
                        sb.append(",");
                    }
        }
        sb.append("}");
        return sb.toString();
    }
}
